package com.heima.kafka.controller;

import com.alibaba.fastjson.JSON;
import com.heima.kafka.pojos.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {
    @Autowired
    private KafkaTemplate<String, String> template;

    @GetMapping("send")
    public String sendMsg(){
        template.send("itcast-topic","黑马程序员");
        return "success";
    }

    @GetMapping("sendObject")
    public String sendObject(){
        User user = new User();
        user.setUsername("xiaowang");
        user.setAge(22);

        template.send("user-topic", JSON.toJSONString(user));
        return "success";
    }
}
